package cn.cs.service.impl;

import cn.cs.client.AgentOrderClient;
import cn.cs.client.CsPartsClient;
import cn.cs.common.Constants;
import cn.cs.common.EmptyUtils;
import cn.cs.common.Page;
import cn.cs.pojo.AgentOrder;
import cn.cs.pojo.CsParts;
import cn.cs.service.AgentOrderService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
public class AgentOrderServiceImpl implements AgentOrderService {

    @Resource
    private AgentOrderClient agentOrderClient;

    @Resource
    private CsPartsClient partsClient;

    @Override
    public AgentOrder getAgentOrderById(String id) throws Exception {
        return agentOrderClient.getAgentOrderById(id);
    }
    @Override
    public List<AgentOrder> getAgentOrderListByMap(Map<String, Object> param) throws Exception {
        return agentOrderClient.getAgentOrderListByMap(param);
    }
    @Override
    public Integer getAgentOrderCountByMap(Map<String, Object> param) throws Exception {
        return agentOrderClient.getAgentOrderCountByMap(param);
    }
    @Override
    public Integer addAgentOrder(AgentOrder agentOrder) throws Exception {
        return agentOrderClient.addAgentOrder(agentOrder);
    }
    @Override
    public Integer modifyAgentOrder(AgentOrder agentOrder) throws Exception {
        return agentOrderClient.modifyAgentOrder(agentOrder);
    }
    @Override
    public Integer deleteAgentOrderById(String id) throws Exception {
        return agentOrderClient.deleteAgentOrderById(id);
    }

    @Override
    public Page<AgentOrder> queryAgentOrderPageByMap(Map<String, Object> param, Integer pageNo, Integer pageSize) throws Exception {
        Integer total = agentOrderClient.getAgentOrderCountByMap(param);
        pageNo = EmptyUtils.isEmpty(pageNo) ? Constants.DEFAULT_PAGE_NO : pageNo;
        pageSize = EmptyUtils.isEmpty(pageSize) ? Constants.DEFAULT_PAGE_SIZE : pageSize;
        Page page = new Page(pageNo, pageSize, total);
        param.put("beginPos", page.getBeginPos());
        param.put("pageSize", page.getPageSize());
        List<AgentOrder> agentOrderList = agentOrderClient.getAgentOrderListByMap(param);
        for(AgentOrder a:agentOrderList){
            String partsId = a.getPartsId();
            CsParts parts = partsClient.getCsPartsById(partsId);
            a.setFactory_name(parts.getFactory_name());
            a.setParts_name(parts.getPartsName());
            a.setParts_price(parts.getPartsPrice());
            a.setParts_model(parts.getPartsModel());
            a.setParts_number(parts.getPartsNumber());
        }
        page.setRows(agentOrderList);
        return page;
    }

}
